package com.holyn.guofang.viewcommon.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.holyn.guofang.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/8.
 */

public class TaskPayDialog extends Dialog {
    private Context context;
    public static final int STATUS_PASSWORD = 0;
    public static final int STATUS_VALID_CODE = 1;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_code_status)
    TextView tvCodeStatus;
    @BindView(R.id.et_code_value)
    EditText etCodeValue;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private OnSureClickListener onSureClickListener = null;
    private OnClickListener onStatusClickListener = null;

    public interface OnSureClickListener{
        public void onSureClick(String value);
    }

    public TaskPayDialog(@NonNull Context context, int status) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        this.context = context;
        initView(status);
    }

    public TaskPayDialog(@NonNull Context context, int status, OnSureClickListener onSureClickListener) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        this.context = context;
        initView(status);
        this.onSureClickListener = onSureClickListener;
    }

    public TaskPayDialog(@NonNull Context context, int status, OnClickListener onStatusClickListener, OnSureClickListener onSureClickListener) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        this.context = context;
        initView(status);
        this.onStatusClickListener = onStatusClickListener;
        this.onSureClickListener = onSureClickListener;
    }

    private void initView(int status) {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_task_pay, null);
        setContentView(dialogView);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);

        if (status == STATUS_PASSWORD){
            tvCodeStatus.setText("密码验证");
            tvCodeStatus.setBackgroundColor(Color.WHITE);
            tvCodeStatus.setClickable(false);
        } else if (status == STATUS_VALID_CODE){
            tvCodeStatus.setText("获取验证码");
            tvCodeStatus.setBackgroundResource(R.drawable.shape_bg_white_sol_grey_stro);
            tvCodeStatus.setClickable(true);
        }
    }

    public OnSureClickListener getOnSureClickListener() {
        return onSureClickListener;
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    @OnClick({R.id.tv_sure, R.id.tv_cancel, R.id.tv_code_status})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                if (onSureClickListener != null) {
                    String value = etCodeValue.getText().toString().trim();
                    if (TextUtils.isEmpty(value)){
                        Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
                    } else {
                        onSureClickListener.onSureClick(value);
                    }
                } else {
                    TaskPayDialog.this.dismiss();
                }
                break;
            case R.id.tv_cancel:
                TaskPayDialog.this.dismiss();
                break;
            case R.id.tv_code_status:
                if (onStatusClickListener != null){
                    onStatusClickListener.onClick(TaskPayDialog.this, 0);
                }
                break;
        }
    }
}
